package com.adinnet.api.child.service.impl;

import com.adinnet.api.appInform.mapper.AppInformMapper;
import com.adinnet.api.appInform.model.AppInform;
import com.adinnet.api.child.model.Child;
import com.adinnet.api.child.mapper.ChildMapper;
import com.adinnet.api.child.model.vo.*;
import com.adinnet.api.child.service.ChildService;
import com.adinnet.api.peaseRecord.mapper.PeaseRecordMapper;
import com.adinnet.api.peaseRecord.model.PeaseRecord;
import com.adinnet.api.peaseRule.mapper.PeaseRuleMapper;
import com.adinnet.common.peaseRule.enums.PeaseRule;
import com.adinnet.common.utils.StringUtil;
import com.adinnet.core.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 儿童表 服务实现类
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-02
 */
@Service
public class ChildServiceImpl extends ServiceImpl<ChildMapper, Child> implements ChildService {


    @Autowired
    private ChildMapper childMapper;

    @Autowired
    private PeaseRecordMapper peaseRecordMapper;

    @Autowired
    private AppInformMapper appInformMapper;

    @Autowired
    private PeaseRuleMapper peaseRuleMapper;

    /**
     * 登陆后-查询宝宝信息
     *
     * @param id
     */
    @Override
    public List<ChildResp> details(Integer id) {
        return childMapper.details(id);
    }

    /**
     * 确认宝宝信息
     *
     * @param id
     */
    @Override
    public void confirm(Integer id) {
        Child child = childMapper.selectById(id);
        if (null == child) {
            throw new BizException("请确认信息是否正确!");
        }
        child.setStatus(1);
        childMapper.updateById(child);
    }

    /**
     * 查询宝宝信息
     */
    @Override
    public ChildDetailsResp information(Integer id) {
        ChildDetailsResp information = childMapper.information(id);
        if (null == information) {
            return null;
        }
        QueryWrapper<AppInform> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("child_id", id);
        List<AppInform> appInforms = appInformMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(appInforms)) {
            information.setWhetherRead(false);
            information.setUnreadRead(0l);
            information.setReadRead(0l);
        } else {
            long unreadRead = appInforms.stream().filter(appInform -> appInform.getStatus() == 1).count();
            if (0 == unreadRead) {
                information.setWhetherRead(false);
                information.setUnreadRead(0l);
                information.setReadRead(appInforms.stream().filter(appInform -> appInform.getStatus() == 2).count());
            } else {
                information.setWhetherRead(true);
                information.setUnreadRead(unreadRead);
                information.setReadRead(appInforms.stream().filter(appInform -> appInform.getStatus() == 2).count());
            }
        }
        if (StringUtils.isNotEmpty(information.getBirthday()) && StringUtils.isNotEmpty(information.getHeadUrl()) && StringUtils.isNotEmpty(information.getNickName())) {
            //不显示
            information.setDataButton(1);
        } else {
            information.setDataButton(0);
        }

        //个人消息
        return information;
    }

    @Override
    @Transactional
    public void edit(Child child) {
        Child c = childMapper.selectById(child.getId());
        if (null == c) {
            throw new BizException("请确认信息是否正确!");
        }
        //首次编辑头像
        if(StringUtil.isBlank(c.getHeadUrl())&&!StringUtil.isBlank(child.getHeadUrl())){
            PeaseRecord peaseRecord1 = new PeaseRecord();
            peaseRecord1.setType(1);
            peaseRecord1.setChildId(c.getId());
            peaseRecord1.setPeaseNum(peaseRuleMapper.getPeaseRuleNumByCode(PeaseRule.UPDATEHEAD.getCode()));
            peaseRecord1.setSource(7);
            peaseRecord1.setSourceName(PeaseRule.UPDATEHEAD.getMessage());
            peaseRecordMapper.insert(peaseRecord1);
        }
        //首次编辑姓名
        if(StringUtil.isBlank(c.getNickName())&&!StringUtil.isBlank(child.getNickName())){
            PeaseRecord peaseRecord2 = new PeaseRecord();
            peaseRecord2.setType(1);
            peaseRecord2.setChildId(c.getId());
            peaseRecord2.setPeaseNum(peaseRuleMapper.getPeaseRuleNumByCode(PeaseRule.UPDATENICKNAME.getCode()));
            peaseRecord2.setSource(8);
            peaseRecord2.setSourceName(PeaseRule.UPDATENICKNAME.getMessage());
            peaseRecordMapper.insert(peaseRecord2);
        }
        //首次编辑生日
        if(StringUtil.isBlank(c.getBirthday())&&!StringUtil.isBlank(child.getBirthday())){
            PeaseRecord peaseRecord3 = new PeaseRecord();
            peaseRecord3.setType(1);
            peaseRecord3.setChildId(c.getId());
            peaseRecord3.setPeaseNum(peaseRuleMapper.getPeaseRuleNumByCode(PeaseRule.UPDATEBIRTHDAY.getCode()));
            peaseRecord3.setSource(9);
            peaseRecord3.setSourceName(PeaseRule.UPDATEBIRTHDAY.getMessage());
            peaseRecordMapper.insert(peaseRecord3);
        }
        childMapper.updateById(child);
    }

    @Override
    public ChildRankPageResp getChildRankList(ChildRankPageReq req) {
        ChildRankPageResp pageResp = new ChildRankPageResp();
        Integer pageNum=req.getLimit()*(req.getPage()-1);
        List<ChildRankResp> records=childMapper.getChildRankList(req.getLimit(),pageNum);
        ChildRankResp myRank=childMapper.getChildRank(req.getChildId());
        pageResp.setChildRank(myRank);
        pageResp.setRecords(records);
        return pageResp;
    }

    /**
     * @param childId:
     * @Description: 清除待分享豆豆数
     * @author zYshuai
     * @Date: 2021/1/18 11:35
     * @return: void
     */
    @Override
    public void clearSharePeaseNum(Integer childId) {
        Child child = new Child();
        child.setSharePeaseNum(0);
        childMapper.update(child, new QueryWrapper<Child>().eq("id", childId));
    }
}
